package com.sysu.minsysu;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import com.sysu.minsysu.map.GPSManager;
import com.sysu.minsysu.map.Maps;
import com.sysu.minsysu.wdzj.WoDeZuJi;
import com.sysu.minsysu.xyxw.XiaoYuanXiaoWu;
import com.sysu.minsysu.zdkb.ZhongDaKeBiao;
import com.sysu.minsysu.zdkb.utils.AnimationTabHost;
import com.sysu.minsysu.zxcx.ZiXunChaXun;
import com.sysu.R;

public class Main extends TabActivity implements OnTabChangeListener {
	
	private final static String TAG = "Main";
	
//	private final static String TAB_ZXCX = "zxcx";
//	private final static String TAB_MAP = "map";
//	private final static String TAB_XYXW = "xyxw";
//	private final static String TAB_ZDKB = "zdkb";
//	private final static String TAB_SZZD = "szzd";

	private GestureDetector gestureDetector;
	private FrameLayout frameLayout;

	private AnimationTabHost mTabHost;
	private TabWidget mTabWidget;
	
	/** 记录当前分页ID */
	private int currentTabID = 0;
//	TabHost mTabHost;
//	TabHost.TabSpec mSpec;
	
//	int mCurSelect=0;
	
//	int[] mCircleX=null;
//	int mCircleHeight;
//	ImageView[] mCircles;
//	TabWidgetView[] mTabViews;
	
	int mScreenHeight;
	int mScreenWidth;
	
	public static GPSManager mGpsManager;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main);
        //getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
//        mCircles = new ImageView[5];
//        mCircles[0] = (ImageView)findViewById(R.id.circle1);
//        mCircles[1] = (ImageView)findViewById(R.id.circle2);
//        mCircles[2] = (ImageView)findViewById(R.id.circle3);
//        mCircles[3] = (ImageView)findViewById(R.id.circle4);
//        mCircles[4] = (ImageView)findViewById(R.id.circle5);
//        
//        mTabViews = new TabWidgetView[5];
//        mTabViews[0] = new TabWidgetView(this, R.drawable.zixunchaxun, "资讯查询");
//        mTabViews[1] = new TabWidgetView(this, R.drawable.map, "地图功能");
//        mTabViews[2] = new TabWidgetView(this, R.drawable.xiaoyuanxiaowu, "校园校务");
//        mTabViews[3] = new TabWidgetView(this, R.drawable.zhongdakebiao, "中大课表");
//        mTabViews[4] = new TabWidgetView(this, R.drawable.shuzizhongda, "数字中大");
                
        Display display = getWindowManager().getDefaultDisplay();
        mScreenHeight = display.getHeight();
        mScreenWidth = display.getWidth();
        
//        Intent intent = new Intent(this, ZiXunChaXun.class);
//        mTabHost = getTabHost();
//        mSpec = mTabHost.newTabSpec(TAB_ZXCX)
//        				.setIndicator( mTabViews[0] )
//        				.setContent(intent);
//        mTabHost.addTab(mSpec);
//        
//        intent = new Intent(this, Maps.class);
//        mSpec = mTabHost.newTabSpec(TAB_MAP)
//						.setIndicator( mTabViews[1] )
//        				.setContent(intent);
//        mTabHost.addTab(mSpec);
//        
//        intent = new Intent( this, XiaoYuanXiaoWu.class);
//        mSpec = mTabHost.newTabSpec(TAB_XYXW)
//						.setIndicator( mTabViews[2] )
//        				.setContent(intent);
//        mTabHost.addTab(mSpec);     
//
//        intent = new Intent( this, ZhongDaKeBiao.class);
//        mSpec = mTabHost.newTabSpec(TAB_ZDKB)
//						.setIndicator( mTabViews[3] )
//        				.setContent(intent);
//        mTabHost.addTab(mSpec);     
//
//        intent = new Intent( this, ShuZiZhongDa.class);
//        mSpec = mTabHost.newTabSpec(TAB_SZZD)
//						.setIndicator( mTabViews[4] )
//        				.setContent(intent);
//        mTabHost.addTab(mSpec);    
//        
//        mTabHost.setOnTabChangedListener( new MyOnTabChangeListener());
        
        mTabHost = (AnimationTabHost) findViewById(android.R.id.tabhost);
		mTabWidget = (TabWidget) findViewById(android.R.id.tabs);
		mTabHost.setOnTabChangedListener(this);

		init();

		gestureDetector = new GestureDetector(new TabHostTouch());
		new View.OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				if (gestureDetector.onTouchEvent(event)) {
					return true;
				}
				return false;
			}
		};

		frameLayout = mTabHost.getTabContentView();
		Log.i("TabHostActivity",
				"TabHostActivity====>>>onCreate()===>>>>frameLayout: "
						+ frameLayout.getChildCount());
        
        
        mGpsManager = new GPSManager(this);
        if( !mGpsManager.isGPSEnable() ){
        	Toast.makeText(this, "GPS尚未打开，无法获得当前位置", Toast.LENGTH_SHORT).show();
        }
//		mCircles[0].setVisibility(View.INVISIBLE);
//		mTabViews[0].setBackgroundResource(R.drawable.kuang_2);
    }
    
    @Override
    public void onBackPressed(){
//    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
//		builder.setTitle("您确定要退出minSYSU吗？");
//		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//			
//			@Override
//			public void onClick(DialogInterface dialog, int which) {
//				finish();
//			}
//		});
//		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//			
//			@Override
//			public void onClick(DialogInterface dialog, int which) {
//				
//			}
//		});
//		builder.create().show();
    }

    
    @Override
    public boolean onKeyDown( int keyCode, KeyEvent event ){
    	if( event.getKeyCode() == KeyEvent.KEYCODE_BACK ){
    		AlertDialog.Builder builder = new AlertDialog.Builder(this);
    		builder.setTitle("提示");
    		builder.setMessage("您确定要退出minSYSU?");
    		builder.setOnKeyListener( new DialogInterface.OnKeyListener() {
				
				@Override
				public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
					if( keyCode==KeyEvent.KEYCODE_BACK && event.getAction()==KeyEvent.ACTION_DOWN ){
						finish();
						return true;
					}else {
						return false;
					}
				}
			});
    		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			});
    		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					
				}
			});
    		builder.create().show();
    		return true;
    	}else {
			return super.onKeyDown(keyCode, event);
		}
    }
    
    @Override
    public void onDestroy(){
    	if( mGpsManager!=null )
    		mGpsManager.stopGps();
    	super.onDestroy();
    }
    
//    class MyOnTabChangeListener implements TabHost.OnTabChangeListener{
//
//		@Override
//		public void onTabChanged(String tabId) {
//			if( mCircleX==null ){
//	    		mCircleX=new int[5];
//	    		for( int i=0; i<5; i++ ){
//	            	mCircleX[i] = mCircles[i].getLeft();
//	                Log.e(TAG, i + " " + mCircleX[i]);
//	            }
//	    	}
//			
//			Animation animation;
//			int target;
//			if( tabId.equals(TAB_ZXCX)){
//				target = 0;
//				animation = getNowAnimation(0);
//			}else if (tabId.equals(TAB_MAP)) {
//				target = 1;
//				animation = getNowAnimation(1);
//			}else if (tabId.equals(TAB_XYXW)) {
//				target = 2;
//				animation = getNowAnimation(2);
//			}else if (tabId.equals(TAB_ZDKB)) {
//				target = 3;
//				animation = getNowAnimation(3);
//			}else {
//				target = 4;
//				animation = getNowAnimation(4);
//			}
//			//mCircles[mCurSelect].startAnimation(animation);
//			mTabViews[mCurSelect].setBackgroundResource(R.drawable.kuang_1);
//			mTabViews[target].setBackgroundResource(R.drawable.kuang_2);
//			mCurSelect = target;
//		}    	
//    }
//    
//    Animation getNowAnimation( final int target ){
//    	//Log.e(TAG, "target:" + target + " cur:" + mCurSelect);
//    	if( target==mCurSelect )
//    		return null;
//    	
//    	TranslateAnimation animation;
//    	if( target-mCurSelect>0 )
//    		 animation = new TranslateAnimation(0, mCircleX[target-mCurSelect], 0, 0);
//    	else {
//			animation = new TranslateAnimation(0, -mCircleX[mCurSelect-target], 0, 0);
//		}
//    	animation.setInterpolator( AnimationUtils.loadInterpolator(Main.this,
//                android.R.anim.decelerate_interpolator) );
//    	animation.setDuration(500);
//    	
//    	animation.setAnimationListener( new AnimationListener() {
//			
//			@Override
//			public void onAnimationStart(Animation animation) {
//			// TODO Auto-generated method stub
//			
//			}
//			
//			@Override
//			public void onAnimationRepeat(Animation animation) {
//			// TODO Auto-generated method stub
//			
//			}
//			
//			@Override
//			public void onAnimationEnd(Animation animation) {
//				//mCircles[mCurSelect].setVisibility(View.INVISIBLE);
//				//mCircles[target].setVisibility(View.VISIBLE);
//				mCurSelect = target;
//			}
//		});
//    	
//    	return animation;
//    }
    private void init() {
		setIndicator(R.drawable.zixungongxiang, 0, new Intent(this,
				ZiXunChaXun.class), "资讯共享");
		setIndicator(R.drawable.tuguanxiaojing, 1, new Intent(this,
				Maps.class), "地图功能");
		setIndicator(R.drawable.wodezuji, 2, new Intent(this,
				WoDeZuJi.class), "我的足迹");
		
		setIndicator(R.drawable.gerenkebiao, 3, new Intent(this,
				ZhongDaKeBiao.class), "中大课表");
//		setIndicator(R.drawable.gerenzhongxin, 4, new Intent(this,
//				ShuZiZhongDa.class), "数字中大");
		setIndicator(R.drawable.xiaoyuanwenhua, 4, new Intent(this,
				XiaoYuanXiaoWu.class), "校园校务");
		

		mTabHost.setOpenAnimation(true);
	}

	private void setIndicator(int icon, int tabId, Intent intent, String text) {

		View localView = LayoutInflater.from(this.mTabHost.getContext())
				.inflate(R.layout.tab_widget_view, null);
		((ImageView) localView.findViewById(R.id.main_activity_tab_image))
				.setBackgroundResource(icon);
		
//		((TextView) localView.findViewById(R.id.main_activity_tab_text))
//				.setText(text);
		String str = String.valueOf(tabId);

		TabHost.TabSpec localTabSpec = mTabHost.newTabSpec(str).setIndicator(
				localView).setContent(intent);
		mTabHost.addTab(localTabSpec);
	}

	public void onTabChanged(String tabId) {
		int tabID = Integer.valueOf(tabId);

		for (int i = 0; i < mTabWidget.getChildCount(); i++) {
			if (i == tabID) {
//				mTabWidget.getChildAt(Integer.valueOf(i)).setBackgroundColor(
//						Color.YELLOW);
				mTabWidget.getChildAt(Integer.valueOf(i))
					.setBackgroundResource(R.drawable.buttom1_change2);
			} else {
				mTabWidget.getChildAt(Integer.valueOf(i))
						.setBackgroundResource(R.drawable.main_meun_bg);
			}
		}
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {

		if (gestureDetector.onTouchEvent(event)) {
			event.setAction(MotionEvent.ACTION_CANCEL);
		}

		return super.dispatchTouchEvent(event);

	}

	private class TabHostTouch extends SimpleOnGestureListener {
		/** 滑动翻页所需距离 */
		private static final int ON_TOUCH_DISTANCE = 110;

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			if (e1.getX() - e2.getX() <= (-ON_TOUCH_DISTANCE)) {
				currentTabID = mTabHost.getCurrentTab() - 1;

				if (currentTabID < 0) {
					currentTabID = mTabHost.getTabCount() - 1;
				}
			} else if (e1.getX() - e2.getX() >= ON_TOUCH_DISTANCE) {
				currentTabID = mTabHost.getCurrentTab() + 1;

				if (currentTabID >= mTabHost.getTabCount()) {
					currentTabID = 0;
				}
			}
			mTabHost.setCurrentTab(currentTabID);
			return false;
		}
	}
    
}